/*
 * Sesame Inc.
 * Copyright (c) 2009 All Rights Reserved.
 */
package com.sesame.controller;

/**
 * @author r6c
 *
 */

import java.util.Map;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.sesame.toolkit.Encrypt;
import com.sesame.toolkit.IdentifyCode;

public class ReadImageController implements Controller {

    private String imageBase;

    public void setImageBase(String imageBase) {
        this.imageBase = imageBase;
    }

    public ModelAndView handleRequest(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        String id = request.getParameter("id");
        ServletOutputStream output = response.getOutputStream();
        String normalPath = Encrypt.MapDecrypt(id) + ".jpg";
        String noImagePath = "noimage.jpg";
        String noRightPath = "noright.jpg";
        File file = null;
        BufferedImage image = null;

        // System.out.println(normalPath);

        // 设置页面不缓存
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        try {
            // 读取图像文件
            file = new File(imageBase + normalPath);
            if (!file.canRead()) {
                file = new File(imageBase + noImagePath);
            }
            image = ImageIO.read(file);
            // 输出图象到页面
            ImageIO.write(image, "jpeg", output);
            output.close();
        } catch (Exception e) {
        }
        return null;
    }

}